//----------------------------------------------------------------------------
// Anti-Grain Geometry - Version 2.4
// Copyright (C) 2002-2005 Maxim Shemanarev (http://www.antigrain.com)
//
// C# Port port by: Lars Brubaker
//                  larsbrubaker@gmail.com
// Copyright (C) 2007
//
// Permission to copy, use, modify, sell and distribute this software 
// is granted provided this copyright notice appears in all copies. 
// This software is provided "as is" without express or implied
// warranty, and with no claim as to its suitability for any purpose.
//
//----------------------------------------------------------------------------
// Contact: mcseem@antigrain.com
//          mcseemagg@yahoo.com
//          http://www.antigrain.com
//----------------------------------------------------------------------------
//
// classes cbox_ctrl
//
//----------------------------------------------------------------------------
using System;
using AGG;
using AGG.VertexSource;

namespace AGG.UI
{
    //----------------------------------------------------------cbox_ctrl_impl
    public class CheckBoxWidget : GUIWidget
    {
        public delegate void CheckedStateChangedEventHandler(object sender, EventArgs e);
        public event CheckedStateChangedEventHandler CheckedStateChanged;

        private bool     isChecked;

        private RGBA_Bytes m_inactive_color;
        private RGBA_Bytes m_active_color;

        TextWidget statusTextWidget;
        TextWidget labelTextWidget;

        public CheckBoxWidget(double x, double y, string label)
        {
            Location = new Vector2D(x, y);
            statusTextWidget = new TextWidget(isChecked ? "1 - " : "0 - ", 12);
            AddChild(statusTextWidget);
            Checked = false;

            labelTextWidget = new TextWidget(label, 12);
            labelTextWidget.Location = new Vector2D(statusTextWidget.Bounds.Width, 0);
            AddChild(labelTextWidget);

            rect_d statusBounds = statusTextWidget.Bounds;
            rect_d labelBounds = labelTextWidget.Bounds;
            labelBounds.Offset(labelTextWidget.Location.x, 0);
            rect_d union = new rect_d();
            union.unite_rectangles(statusBounds, labelBounds);

            Bounds = union;

            m_inactive_color = new RGBA_Bytes(0.0, 0.0, 0.0);
            m_active_color = new RGBA_Bytes(0.4, 0.0, 0.0);

            MouseDown += ChangeStatus;
        }

        public void SetFontSize(double fontSize)
        {
            // TODO: Need to set the point size of the font and recalculate the bounds
            Console.WriteLine("Warning: Need to set the point size of the font and recalculate the bounds");
        }

        public void SetFontSizeAndWidthRatio(double fontSize, double widthRatioOfHeight)
        {
            throw new NotImplementedException();
        }

        public override String Text
        {
            get
            {
                return labelTextWidget.Text;
            }

            set
            {
                labelTextWidget.Text = value;
            }
        }

        public bool Checked
        {
            get
            {
                return isChecked;
            }

            set
            {
                if (isChecked != value)
                {
                    isChecked = value;
                    statusTextWidget.Text = isChecked ? "1 - " : "0 - ";
                    Invalidate();
                }
            }
        }

        void ChangeStatus(object sender, MouseEventArgs mouseEvent)
        {
            Checked = !Checked;
            if (CheckedStateChanged != null)
            {
                CheckedStateChanged(this, mouseEvent);
            }
        }

        override public void OnKeyDown(KeyEventArgs keyEvent)
        {
            if(keyEvent.KeyCode == Keys.Space)
            {
                isChecked = !isChecked;
                keyEvent.Handled = true;
            }

            base.OnKeyDown(keyEvent);
        }

        public RGBA_Bytes TextColor
        {
            get
            {
                return labelTextWidget.TextColor;
            }

            set
            {
                statusTextWidget.TextColor = value;
                labelTextWidget.TextColor = value;
            }
        }

        public void inactive_color(IColorType c) { m_inactive_color = c.GetAsRGBA_Bytes(); }
        public void active_color(IColorType c) { m_active_color = c.GetAsRGBA_Bytes(); }
    };
}
